using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS.Mappings;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_CMS_Mapping_ui_Form : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxCMSMapAccess"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the state of the listing view
	/// </summary>
	public string ListingState
	{
		get
		{
			return (string)ViewState["ListingState"];
		}
		set
		{
			ViewState["ListingState"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the record being edited
	/// </summary>
	public int RecordID
	{
		get
		{
			object o = ViewState["RecordID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["RecordID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.ListingState = GetAttribute("state", "");
			this.RecordID = GetAttribute("ID", -1);

			// Load the record if specified
			if (this.RecordID != -1)
			{
				DomainMappings mappings = new DomainMappings();
				DomainMapping map = mappings.GetByID( this.RecordID );
				if (map == null) GoBack();

				Domain.Text = map.Domain;
				FK_Rootpage.NodeID = map.FK_Rootpage;
				FK_Homepage.NodeID = map.FK_Homepage;
			}
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to the listing, restoring state
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format("default.aspx?state={0}", this.ListingState);
		Response.Redirect(url, true);
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User cancelled the save, go back to previous page
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, store the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;
		DomainMappings mappings = new DomainMappings();
		DomainMapping map = null;

		if (this.RecordID != -1)
		{
			// Load the existing record and ensure it still exists
			map = mappings.GetByID( this.RecordID );
			if( map == null ) GoBack();
		}
		else
			map = new DomainMapping();

		// Set fields and save
		map.Domain = this.Domain.Text;
		map.FK_Rootpage = this.FK_Rootpage.NodeID;
		map.FK_Homepage = this.FK_Homepage.NodeID;
		mappings.Save( map );

		// Audit
		QuickAudit.RaiseInformation(
			new AuditUser(),
			"Flux", "Mapping Manager",
			"created/edited mapping {0} ({1})",
			map.PK_ID, map.Domain);
		
		GoBack();
	}
	#endregion
}
